package com.example.httpclient.utils;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Map;

public class MyHttpClientUtils {
    private static final String CHARSET = "UTF-8";
    /**
     * httpClient 发送get 请求
     *
     * @param url url
     * @return String
     */
    public static String get(String url) {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet get = new HttpGet(url);
        CloseableHttpResponse response = null;
        String content = "";
        try {
            response = client.execute(get);
            int statusCode = response.getStatusLine().getStatusCode();
            System.out.println("返回的响应码值：" + statusCode);
            HttpEntity entity = response.getEntity();
            content = EntityUtils.toString(entity, CHARSET);
            response.close();
            client.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return content;
    }

    /**
     * 带参数发送get 请求
     *
     * @param url url
     * @param parameter parameter
     * @return String
     */
    public static String doGetAndParam(String url, String parameter) {
        String content = null;
        CloseableHttpClient httpClient = HttpClients.createDefault();
        URIBuilder builder = null;
        try {
            builder = new URIBuilder(url);
            builder.addParameter("query", parameter);
            HttpGet get = new HttpGet(builder.build());
            CloseableHttpResponse response = httpClient.execute(get);
            int statusCode = response.getStatusLine().getStatusCode();
            System.out.println("doGetAndParam 返回的状态码是："+ statusCode);
            HttpEntity entity = response.getEntity();
            content = EntityUtils.toString(entity, CHARSET);
            response.close();
            httpClient.close();
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        } catch (ClientProtocolException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return content;
    }

    /**
     * post 请求
     *
     * @param url url
     * @return String
     */
    public static String post(String url) {
        String content = null;
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        try {
            CloseableHttpResponse response = client.execute(httpPost);
            int statusCode = response.getStatusLine().getStatusCode();
            System.out.println("post 请求获取到的响应码值：" + statusCode);
            HttpEntity entity = response.getEntity();
            content = EntityUtils.toString(entity, CHARSET);
            response.close();
            client.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return content;
    }


    /**
     * 带参数发送post 请求
     *
     * @param url url
     * @param map map
     * @return String
     */
    public static String doPostAndParam(String url, Map<String, String> map) {
        String content = null;
        CloseableHttpClient client = HttpClients.createDefault(); // 实例化httpClient 对象
        HttpPost post = new HttpPost(url); // 实例化一个httpPost 对象
        // 请求参数创建
        ArrayList<NameValuePair> list = new ArrayList<>();
        list.add(new BasicNameValuePair("userName", (String) map.get("userName")));
        list.add(new BasicNameValuePair("password", (String) map.get("password")));
        //包装成一个entity对象
        StringEntity entity = null;
        try {
            entity = new UrlEncodedFormEntity(list,"utf-8");
            //设置请求内容
            post.setEntity(entity);
            //执行请求内容
            CloseableHttpResponse response = client.execute(post);
            int code = response.getStatusLine().getStatusCode();
            HttpEntity contentEntity = response.getEntity();
            content = EntityUtils.toString(contentEntity);
            System.out.print("doPostAndParam 请求接口响应码: " + code);
            System.out.print("doPostAndParam 请求接口返回报文:" + content);
            response.close();
            client.close();
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        } catch (ClientProtocolException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return content;
    }
}
